package com.patzn.osgi.helloworld;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

public class HelloWorldActivator implements BundleActivator {
    private ServiceRegistration<Runnable> serviceRegistration;

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("bundle[HelloWorld] start...");

        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("vendor", "hello");
        serviceRegistration = bundleContext.registerService(Runnable.class, () -> {}, properties);

        // 10秒之后，修改 属性值
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Dictionary<String, Object> properties = new Hashtable<>();
                properties.put("vendor", "world");
                serviceRegistration.setProperties(properties);
            }
        }, 1000);
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("bundle[HelloWorld] stop...");

        serviceRegistration.unregister();
    }
}
